﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace bttuan7
{
    public partial class welcome : Form
    {
        public my_users current_user;
        public welcome()
        {
            InitializeComponent();

            // them cot vao list view
            ltv_user_list.Columns.Add("STT", 100, HorizontalAlignment.Left);
            ltv_user_list.Columns.Add("Tài khoản", 100, HorizontalAlignment.Left);
            ltv_user_list.Columns.Add("Họ và tên", 100, HorizontalAlignment.Left);
            ltv_user_list.Columns.Add("Quyền", 100, HorizontalAlignment.Left);

            ltv_user_list.View = View.Details;

            OpenDatabase();

            cbb_quyen.Items.Add("admin");
            cbb_quyen.Items.Add("user");
        }

        private void OpenDatabase()
        {
            ltv_user_list.Items.Clear();
            int i = 1;
            foreach (my_users user in my_users.get_users())
            {
                ListViewItem lvi = ltv_user_list.Items.Add(i.ToString()); i++;
                lvi.Tag = user;
                lvi.SubItems.Add(user.user_name);
                lvi.SubItems.Add(user.full_name);
                lvi.SubItems.Add(user.group_id);
            }
        }


        private void welcome_Load(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            frm.ShowDialog();

            if (frm.dang_nhap_thanh_cong)
            {
                my_users user = frm.this_user;

                if (user.group_id == "admin")
                {
                    ts_lb_quan_ly_nguoi_dung.Visible = true;
                    ts_lb_quan_ly_nghiep_vu.Visible = false;
                }
                else if (user.group_id == "user")
                {
                    ts_lb_quan_ly_nghiep_vu.Visible = true;
                    ts_lb_quan_ly_nguoi_dung.Visible = false;
                }
            }
            else
            {
                this.Close();
            }
        }

        private void ltv_user_list_ItemActivate(object sender, EventArgs e)
        {
            my_users user = (my_users)ltv_user_list.SelectedItems[0].Tag;
            tb_tai_khoan.Text = user.user_name;
            tb_ho_ten.Text = user.full_name;
            if (cbb_quyen.Items[0].ToString() == user.group_id)
                cbb_quyen.SelectedIndex = 0;
            else
                cbb_quyen.SelectedIndex = 1;
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_cap_nhap_Click(object sender, EventArgs e)
        {
            my_users cur_user = (my_users)ltv_user_list.SelectedItems[0].Tag;
            string key = cur_user.user_name;
            cur_user.user_name = tb_tai_khoan.Text;
            cur_user.full_name = tb_ho_ten.Text;
            cur_user.group_id = cbb_quyen.SelectedItem.ToString();
            cur_user.write_down(key);
            ltv_user_list.Items.Clear();
            OpenDatabase();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            my_users user_to_delete = (my_users)ltv_user_list.SelectedItems[0].Tag;
            string name = user_to_delete.user_name;
            my_users.delete(name);
            ltv_user_list.Items.Clear();
            OpenDatabase();
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            my_users user = new my_users();
            user.user_name = tb_tai_khoan.Text;
            user.full_name = tb_ho_ten.Text;
            user.group_id = cbb_quyen.SelectedItem.ToString();
            user.write_down(user.user_name);
            ltv_user_list.Items.Clear();
            OpenDatabase();
        }

        private void btn_change_password_Click(object sender, EventArgs e)
        {
            current_user = (my_users)ltv_user_list.SelectedItems[0].Tag;
            frm_password_change frm_pass_change = new frm_password_change();
            frm_pass_change.frm_welcome = this;
            frm_pass_change.Show();
            current_user.write_down(current_user.user_name);
            ltv_user_list.Items.Clear();
            OpenDatabase();
        }
    }
}
